//
// Created by Administrator on 2022/8/25.
//

#ifndef TPSERVER_TP_TIME_H
#define TPSERVER_TP_TIME_H
#include <time.h>
#include "tp_string.h"
#include "tp_atomic.h"

#define NGX_TIME_SLOTS   64

static int       slot;

static tp_atomic_t      tp_time_lock;

typedef struct tm             tp_tm_t;

#define tp_tm_sec            tm_sec
#define tp_tm_min            tm_min
#define tp_tm_hour           tm_hour
#define tp_tm_mday           tm_mday
#define tp_tm_mon            tm_mon
#define tp_tm_year           tm_year
#define tp_tm_wday           tm_wday
#define tp_tm_isdst          tm_isdst

typedef struct {
    time_t      sec;
    int        msec;
    int       gmtoff;
} tp_time_t;

extern volatile char*        tp_current_msec;
extern volatile tp_time_t  *tp_cached_time;
extern volatile tp_str_t    tp_cached_http_time;
extern volatile tp_str_t    tp_cached_err_log_time;


#define ngx_time()           tp_cached_time->sec
#define ngx_timeofday()      (tp_time_t *) tp_cached_time

#define tp_gettimeofday(tp)  (void) gettimeofday(tp, NULL);
#define tp_msleep(ms)        (void) usleep(ms * 1000)
#define tp_sleep(s)          (void) sleep(s)

long tp_times();
void tp_time_init(void);
void tp_time_update(void);
static char* tp_monotonic_time(time_t sec, int msec);
void tp_gmtime(time_t t, tp_tm_t *tp);
void tp_localtime(time_t s, tp_tm_t *tm);
#endif //TPSERVER_TP_TIME_H
